package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author WZhaokang
 * @date 2020/12/29 16:10
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {

    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private ReturnListDao returnListDao;
    @Autowired
    private GoodsDao goodsDao;


    //退货出库
    @Override
    @Transactional
    public int save(ReturnList returnList, String returnListGoodsStr) {
        int res1 = returnListDao.save(returnList);

        ReturnListGoods[] returnListGoods = new Gson().fromJson(returnListGoodsStr, ReturnListGoods[].class);
        List<ReturnListGoods> returnListGoods1 = Arrays.asList(returnListGoods);
        for (ReturnListGoods listGoods : returnListGoods1) {
            listGoods.setReturnListId(returnList.getReturnListId());
            returnListGoodsDao.save(listGoods);

            //修改库存
            Goods goods = goodsDao.findByGoodsId(listGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity() - listGoods.getGoodsNum());
            goodsDao.updateGoods(goods);
        }
        return res1;
    }


    @Override
    public List<ReturnListGoods> goodList(Integer returnListId) {
        return returnListGoodsDao.goodList(returnListId);
    }
}
